package com.lujianfei.netease_android_mvvm_kotlin.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.lujianfei.netease_android_mvvm_kotlin.R
import com.lujianfei.netease_android_mvvm_kotlin.databinding.AdapterMainFooterBinding
import com.lujianfei.netease_android_mvvm_kotlin.databinding.AdapterMainItemBinding
import com.lujianfei.netease_android_mvvm_kotlin.entity.DataBean
import com.lujianfei.netease_android_mvvm_kotlin.viewmodel.MainViewModel


class MainAdapter(val mViewModel: MainViewModel) :
    RecyclerView.Adapter<ViewHolder>() {

    companion object {
        const val TYPE_ITEM = 0
        const val TYPE_FOOTER = 1
    }

    private val mDataLists = ArrayList<DataBean>()

    private var editMode = false

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var viewHolder: ViewHolder?
        when (viewType) {
            TYPE_ITEM -> {
                val binding = DataBindingUtil.inflate<ViewDataBinding>(
                    LayoutInflater.from(
                        parent.context
                    ),
                    R.layout.adapter_main_item,
                    null,
                    false
                )
                viewHolder = MyViewHolder(binding)
            }
            TYPE_FOOTER -> {
                val binding = DataBindingUtil.inflate<ViewDataBinding>(
                    LayoutInflater.from(
                        parent.context
                    ),
                    R.layout.adapter_main_footer,
                    null,
                    false
                )
                viewHolder = MyFooterHolder(binding)
            }
            else -> {
                val binding = DataBindingUtil.inflate<ViewDataBinding>(
                    LayoutInflater.from(
                        parent.context
                    ),
                    R.layout.adapter_main_item,
                    null,
                    false
                )
                viewHolder = MyViewHolder(binding)
            }
        }
        return viewHolder
    }

    override fun getItemViewType(position: Int): Int {
       return this.mDataLists[position].type
    }

    override fun getItemCount(): Int {
        return this.mDataLists.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            TYPE_ITEM -> {
                val viewHolder = holder as MyViewHolder
                val binding: AdapterMainItemBinding = viewHolder.binding as AdapterMainItemBinding
                val dataList = mDataLists[position]
                binding.itemData = dataList
                binding.editMode = editMode
                binding.viewModel = mViewModel
            }
            TYPE_FOOTER -> {
                val viewHolder = holder as MyFooterHolder
                val binding: AdapterMainFooterBinding =
                    viewHolder.binding as AdapterMainFooterBinding
                binding.editMode = editMode
            }
        }

    }

    fun setEditMode(editMode: Boolean) {
        this.editMode = editMode
        resetChecked()
        notifyItemRangeChanged(0, itemCount)
    }

    fun isEditMode(): Boolean {
        return editMode
    }

    fun resetChecked() {
        for (dataList in mDataLists) {
            dataList.checked = false
        }
    }

    fun setData(dataListBean: MutableList<DataBean>) {
        DiffUtil.calculateDiff(DiffCallback(this.mDataLists,dataListBean),true).dispatchUpdatesTo(this)
        this.mDataLists.clear()
        this.mDataLists.addAll(dataListBean)
    }

    internal inner class MyViewHolder(binding: ViewDataBinding) :
        RecyclerView.ViewHolder(binding.root) {
        var binding: ViewDataBinding? = null

        init {
            this.binding = binding
        }
    }

    internal inner class MyFooterHolder(binding: ViewDataBinding) :
        RecyclerView.ViewHolder(binding.root) {
        var binding: ViewDataBinding? = null

        init {
            this.binding = binding
        }
    }
}

class DiffCallback(private val oldCardList: MutableList<DataBean>,private val newCardList: MutableList<DataBean>) : DiffUtil.Callback() {
    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldCardList[oldItemPosition].id == newCardList[newItemPosition].id
    }

    override fun getOldListSize(): Int {
        return oldCardList.size
    }

    override fun getNewListSize(): Int {
        return newCardList.size
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldCardList[oldItemPosition] == newCardList[newItemPosition]
    }
}